import store from '../../store'
import StockCandle from '@/components/StockCandle/StockCandle.vue'
import StockRank from '@/components/StockRank/StockRank.vue'
import router from '../../router'
import { ElMessage } from '_element-plus@1.0.2-beta.48@element-plus'
import AllStock from '../../assets/AllStock'

export default {
  name: 'Stock',
  components: {
    StockCandle,
    StockRank
  },
  data () {
    var stockCode = '600519.SH'
    var stockName = '贵州茅台'
    var formattedStockCode = 'sh600519'
    var stockIndexData1
    var indexPrice1
    var indexChange1
    var stockIndexData2
    var indexPrice2
    var indexChange2
    var stockIndexData3
    var indexPrice3
    var indexChange3
    var stockData
    var stockPrice
    var stockChange
    return {
      stockCode,
      stockName,
      formattedStockCode,
      stockIndexData1,
      indexPrice1,
      indexChange1,
      stockIndexData2,
      indexPrice2,
      indexChange2,
      stockIndexData3,
      indexPrice3,
      indexChange3,
      stockData,
      stockPrice,
      stockChange
    }
  },
  mounted () {
    this.getStockIndex(this.formattedStockCode)
  },
  methods: {
    getStockIndex (code) {
      store.dispatch('instantStockIndex', code)
        .then(response => {
          this.stockIndexData1 = response.data.上证指数
          var now = this.stockIndexData1.最近成交价
          var yesterday = this.stockIndexData1.昨日收盘价
          this.indexPrice1 = parseFloat(now).toFixed(2)
          this.indexChange1 = Math.round((parseFloat(now) - parseFloat(yesterday)) / parseFloat(yesterday) * 10000) / 100.00.toFixed(2)

          this.stockIndexData2 = response.data.深证成指
          now = this.stockIndexData2.最近成交价
          yesterday = this.stockIndexData2.昨日收盘价
          this.indexPrice2 = parseFloat(now).toFixed(2)
          this.indexChange2 = Math.round((parseFloat(now) - parseFloat(yesterday)) / parseFloat(yesterday) * 10000) / 100.00.toFixed(2)

          this.stockIndexData3 = response.data.创业板指
          now = this.stockIndexData3.最近成交价
          yesterday = this.stockIndexData3.昨日收盘价
          this.indexPrice3 = parseFloat(now).toFixed(2)
          this.indexChange3 = Math.round((parseFloat(now) - parseFloat(yesterday)) / parseFloat(yesterday) * 10000) / 100.00.toFixed(2)

          this.stockData = response.data.选中股票
          this.stockName = this.stockData.证券简称
          now = this.stockData.最近成交价
          yesterday = this.stockData.昨日收盘价
          this.stockPrice = parseFloat(now).toFixed(2)
          this.stockChange = Math.round((parseFloat(now) - parseFloat(yesterday)) / parseFloat(yesterday) * 10000) / 100.00.toFixed(2)
        })
        .catch(error => {
          console.log('connect failed')
          console.log(error)
        })
    },
    changeStock (code, name) {
      this.formattedStockCode = code
      this.getStockIndex(this.formattedStockCode)
      if (code[1] === 'h') {
        code = code.substring(2)
        code = code + '.SH'
      } else if (code[1] === 'z') {
        code = code.substring(2)
        code = code + '.SZ'
      }
      this.stockCode = code
      this.stockName = name
    },
    judgeCode (code) {
      const allStock = AllStock.data().stockData
      var judge = false
      for (let i = 0; i < allStock.length; ++i) {
        if (allStock[i][0] === code) {
          judge = true
          break
        }
      }
      return judge
    },
    clickSearch () {
      if (!this.judgeCode(this.formattedStockCode)) {
        ElMessage.error('请输入正确的股票代码')
        return
      }
      this.getStockIndex(this.formattedStockCode)
      var code = this.formattedStockCode
      if (code[1] === 'h') {
        code = code.substring(2)
        code = code + '.SH'
      } else if (code[1] === 'z') {
        code = code.substring(2)
        code = code + '.SZ'
      }
      this.stockCode = code
      this.stockName = name
    },
    toTrade () {
      router.push({
        path: '/Trade',
        query: {
          stockCode: this.formattedStockCode,
          tradeType: '买入'
        }
      })
    }
  }
}
